/* internal/js/food-item/maintain.js */

/* Common */

var SERVLET_PATH = '/ordrs/internal/maintain-food-item';

/* END Common */


/* Create */

function createFoodItem () {
	createFoodItemValidate();
}

//done
function createFoodItemValidate () {
	var name = document.getElementById('foodItemName').value;
	var description = document.getElementById('foodItemDescription').value;
	var cost = document.getElementById('foodItemCost').value;
	var typeIsNew = false;
	var type;
		var typeInd = document.getElementById('foodItemType').options[document.getElementById('foodItemType').selectedIndex].value;
		if (typeInd == 'new') {
			type = document.getElementById('foodItemNewType').value;
			typeIsNew = true;
		} else
			type = typeInd;
	
	var available = getCheckedValue(document.getElementById('foodItemAvailable'));

	var valid = true;

	if (name == '') { //left blank
		valid = false;
		showFieldRequired('foodItemNameMessage');
	}

	if (description == '') { //left blank
		valid = false;
		showFieldRequired('foodItemDescriptionMessage');
	}

	if (cost == '') { //left blank
		valid = false;
		showFieldRequired('foodItemCostMessage');
	} else if (!cost.match(/^\d*\.\d\d$/)) { //bad format
		valid = false;
		showFieldMessage('foodItemCostMessage', 'Invalid Cost');
	}

	if (type == '') { //left blank
		valid = false;
		showFieldRequired('foodItemTypeMessage');
	} else if (type == 'new') { //"new" was entered in as the new type
		showFieldMessage('foodItemTypeMessage', '"New" is not a valid type');
	}

	if (valid)
		createFoodItemSend(name, description, cost, type, available, typeIsNew);
}

//done
function createFoodItemSend (name, description, cost, type, available, typeIsNew) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('c') +
	            '&name=' + encodeURI(name) +
	            '&description=' + encodeURI(description) +
	            '&cost=' + encodeURI(cost) +
	            '&type=' + encodeURI(type) +
	            '&available=' + encodeURI(available);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				createFoodItemReceive(req.responseXML, typeIsNew);
			} else if (req.status == 204) {
				//can't find server
				createFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}

function createFoodItemReceive (xml, typeIsNew) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		if (typeIsNew) {
			listFoodItemTypes();
			//addType(type);
		}
		listAllFoodItemSend();
		showDialog('Food item created.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Create */


/* Report */

function reportFoodItem () {
	reportFoodItemValidate();
}

//done
function reportFoodItemValidate () {
	var name = document.getElementById('foodItemName').value;
	var type;
		var typeInd = document.getElementById('foodItemType').options[document.getElementById('foodItemType').selectedIndex].value;
		if (typeInd == 'new')
			type = document.getElementById('foodItemNewType').value;
		else
			type = typeInd;
	var available = getCheckedValue(document.getElementById('foodItemAvailable'));

	//TODO always valid because available is always a search field THIS IS WRONG NOW
	reportFoodItemSend(name, type, available);
}

//done
function reportFoodItemSend (name, type, available) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
	            '&name=' + encodeURI(name) +
		        '&type=' + encodeURI(type) +
	            '&available=' + encodeURI(available);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportFoodItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function reportFoodItemReceive (xml) {
	if (xml.getElementsByTagName('fooditems').length > 0) {
		var items = xml.getElementsByTagName('fooditems')[0];
		showResults(buildTable(items));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Report*/


/* Update */

function updateFoodItem () {
	updateFoodItemValidate();
}

function updateFoodItemValidate () {
	var id = document.getElementById('foodItemId').value;
	var name = document.getElementById('foodItemName').value;
	var description = document.getElementById('foodItemDescription').value;
	var cost = document.getElementById('foodItemCost').value;
	var typeIsNew = false;
	var type;
		var typeInd = document.getElementById('foodItemType').options[document.getElementById('foodItemType').selectedIndex].value;
		if (typeInd == 'new') {
			type = document.getElementById('foodItemNewType').value;
			typeIsNew = true;
		} else
			type = typeInd;
	var available = getCheckedValue(document.getElementById('foodItemAvailable'));

	var valid = true;

	if (id == '') { //no item selected
		showDialog('No Menu Item is selected for updating.');
		return;
	}

	if (name == '') { //left blank
		valid = false;
		showFieldRequired('foodItemNameMessage');
	}

	if (description == '') { //left blank
		valid = false;
		showFieldRequired('foodItemDescriptionMessage');
	}

	if (cost == '') { //left blank
		valid = false;
		showFieldRequired('foodItemCostMessage');
	} else if (!cost.match(/^\d*\.\d\d$/)) { //bad format
		valid = false;
		showFieldMessage('foodItemCostMessage', 'Invalid Cost');
	}

	if (type == '') { //left blank
		valid = false;
		showFieldRequired('foodItemTypeMessage');
	}

	if (valid)
		updateFoodItemSend(id, name, description, cost, type, available, typeIsNew);
}

//really done
function updateFoodItemSend (id, name, description, cost, type, available, typeIsNew) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
	            '&id=' + encodeURI(id) +
	            '&name=' + encodeURI(name) +
	            '&description=' + encodeURI(description) +
	            '&cost=' + encodeURI(cost) +
	            '&type=' + encodeURI(type) +
	            '&available=' + encodeURI(available);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateFoodItemReceive(req.responseXML, typeIsNew);
			} else if (req.status == 204) {
				//can't find server
				updateFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}


function updateFoodItemReceive (xml, typeIsNew) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		if (typeIsNew) {
			listFoodItemTypes();
			//addType(type);
		}
		listAllFoodItemSend();
		showDialog('Food item updated.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Update */


/* Delete */

function deleteFoodItem () {
	deleteFoodItemValidate();
}

function deleteFoodItemValidate () {
	var id = document.getElementById('foodItemId').value;
	if (id == '') {
		//no item selected
		showDialog('No food item is selected for deletion.');
	} else {
		//item selected
		deleteFoodItemSend(id);
	}
}


function deleteFoodItemSend (id) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('d') +
	            '&id=' + encodeURI(id);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				deleteFoodItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				deleteFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function deleteFoodItemReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		resetFields();
		listAllFoodItemSend();
		showDialog('Food item deleted.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Delete */


/* List All */

function listAllFoodItem () {
	listAllFoodItemSend();
}

function listAllFoodItemSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('l');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllFoodItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllFoodItemReceive (xml) {
	if (xml.getElementsByTagName('fooditems').length > 0) {
		var items = xml.getElementsByTagName('fooditems')[0];
		showResults(buildTable(items));
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */


/* List Food Item Types */

function listFoodItemTypes () {
	listFoodItemTypesSend();
}

function listFoodItemTypesSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('t');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listFoodItemTypesReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listFoodItemTypesReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listFoodItemTypesReceive (xml) {
	if (xml.getElementsByTagName('orderitemtypes').length > 0) {
		var types = xml.getElementsByTagName('orderitemtypes')[0];
		for (var loop = 0; loop < types.childNodes.length; loop++) {
			var type = types.childNodes[loop];
			var name = type.getElementsByTagName('name')[0].childNodes[0].nodeValue;
			var optgroup = document.getElementById('foodItemTypeOptGroup');
			var opt = document.createElement('option');
			opt.setAttribute('value', name);
			opt.innerHTML = name;
			optgroup.appendChild(opt);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10301)
			showDialog(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List Food Item Types */
